<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
    <title>拟物计算器</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
        * {
            margin: 0;padding: 0;
        }
        body {
            background-color: #eee;
        }
        .container {
            width: 306px;
            min-height: 70vh;
            margin: 50px auto;
            border-radius: 20px;
            padding: 20px;
            box-shadow: -8px -8px 16px -10px rgba(255, 255, 255, 1),8px 8px 16px -10px rgba(0, 0, 0, 0.15);
        }
        .result {
            padding: 20px;
            text-align: right;
            font-size: 30px;
            word-break: break-all;
            overflow-y:scroll;
            text-overflow:ellipsis;
            display:-webkit-box;
            -webkit-line-clamp:2;
            -webkit-box-orient:vertical;
        }
        .btn-group {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
        }
        .btn-group button {
            font-size: 20px;
            margin: 8px;
            color: #7e7e7e;
            width: 60px;
            height: 60px;
            border: 0;
            outline: none;
            border-radius: 50%;
            box-shadow: -4px -4px 10px -8px rgba(255, 255, 255, 1), 4px 4px 10px -8px rgba(0, 0, 0, .3);
            background: linear-gradient(135deg, rgba(230,230,230,1) 0%, rgba(246,246,246,1) 100%);
        }
        button:active{
            box-shadow: -4px -4px 10px -8px rgba(255, 255, 255, 1) inset, 4px 4px 10px -8px rgba(0, 0, 0, .3) inset;
        }
        .btn-group .num-0 {
            width: 120px;
            border-radius: 40px;
        }
    </style>
</head>
<body>
    <div id="app">
    <div class="container">
        <div class="result"> {{ result }} </div>

        <div class="btn-group">
            <button @click="clear">AC</button>
            <button @click="calculateToggle">±</button>
            <button @click="calculatePercentage">%</button>
            <button @click="append('÷')">÷</button>

            <button @click="append(7)">7</button>
            <button @click="append(8)">8</button>
            <button @click="append(9)">9</button>
            <button @click="append('×')">×</button>

            <button @click="append(4)">4</button>
            <button @click="append(5)">5</button>
            <button @click="append(6)">6</button>
            <button @click="append('-')">-</button>

            <button @click="append(1)">1</button>
            <button @click="append(2)">2</button>
            <button @click="append(3)">3</button>
            <button @click="append('+')">+</button>

            <button class="number num-0" @click="append(0)">0</button>
            <button @click="append('.')">.</button>
            <button @click="calculate">=</button>
        </div>

        <audio id="myAudio" :src="audioSrc" autoplay="false"></audio>
    </div>
</div>
</body>
<script>
    // var s = require('sound/1.oga');
    new Vue({
        el: "#app",
        data: {
            audioSrc: "",
            result: 0,
            isDecimalAdded: false, // 判断是否输入过小数点
            isOperatorAdded: false, // 判断是否输入过加减乘除
            isStarted: false,     // 判断是否输入过数字
        },
        methods: {
            // 判断输入的是否是加减乘除
            isOperator(character) {
                return ['+', '-', '×', '÷'].indexOf(character) > -1;
            },
            // 输入数字和小数点
            append(character) {                
                this.playVol(character)
                if(this.result == '0' && !this.isOperator(character)) {
                    if(character == '.') {
                        this.result += ''+ character
                        this.isDecimalAdded = true;
                    }else {
                        this.result = '' + character;
                    }
                    this.isStarted = true;
                    return;                    
                }

                if(!this.isOperator(character)) {
                    if(character == '.' && this.isDecimalAdded) {
                        return;
                    }
                    if(character == '.') {
                        this.isDecimalAdded = true;
                        this.isOperatorAdded = true;
                    }else {
                        this.isOperatorAdded = false;
                    }
                    console.log(this.result)

                    this.result += '' + character;
                }

                if(this.isOperator(character) && !this.isOperatorAdded) {
                    this.result += '' + character;
                    this.isOperatorAdded = true;
                    this.isDecimalAdded = false;
                }
            },
            // = 
            calculate() {
                this.playVol('=')
                if(this.result == 0) {
                    return;
                }
                let res = this.result.replace(new RegExp('×', 'g'), '*').replace(new RegExp('÷', 'g'), '/');
                // console.log(res)
                this.result = parseFloat(eval(res).toFixed(9)).toString();
                this.isOperatorAdded = false;
                this.isDecimalAdded = false;
                this.playVol(this.result)
            },
            // ±
            calculateToggle() {
                this.playVol('±')
                if(this.isOperatorAdded || !this.isStarted) {
                    return;
                }

                this.result = this.result + '* -1';
                this.calculate()
            },
            // %
            calculatePercentage() {
                this.playVol('%')
                if(this.isOperatorAdded || !this.isStarted) {
                    return;
                }

                this.result = this.result + '* 0.01';
                this.calculate()
            },
            // AC
            clear() {
                this.playVol('ac')
                this.result= 0
                this.isDecimalAdded= false
                this.isOperatorAdded= false
                this.isStarted= false
            },
            playVol(msg) {
                const mapping = {
                    '.': '点',
                    'ac': '归零',
                    '±': '正负',
                    '%': '百分号'
                }


                for(k in mapping) {
                    if(k == msg) {
                        msg = mapping[k];
                    }
                }
                let m = new SpeechSynthesisUtterance(msg);
                speechSynthesis.speak(m);
            },
            playSound(character) {
                if(character == '.') {
                    this.audioSrc = `sound/d.m4a`
                }else{
                    this.audioSrc = `sound/${character}.m4a`
                }
            }
        }
    })

</script>
</html>